package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bean.message.efisc.EpayFiscVO;
import com.jishuo.nontax.bfc.service.EpayFiscService;
import org.dom4j.Element;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author hlp
 * @date 2023/2/8 15:16
 * @Description:
 */
public class EpayFiscApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("setPayFiscBatch")) {// 电子缴库接口，批量扣款
            Element bills1 = body.element("bills");
            List<Element> bills = bills1.elements("bill");
            EpayFiscService epayFiscService = (EpayFiscService) SpringContextUtil.getBean("epayFiscService");

            int nRet = 1;
            for (int i=0;i<bills.size();i++) {
                Element ele = bills.get(i);
                EpayFiscVO vo = new EpayFiscVO();
                vo.setBillNum(ele.elementText("BillNum"));
                vo.setTraNo(ele.elementText("TraNo"));
                vo.setAccountCode(ele.elementText("PayerAcct"));
                vo.setPayBankDt(ele.elementText("TraTime"));
                vo.setBankAffirm(ele.elementText("BankTraNo"));
                vo.setBankStation(ele.elementText("BankStation"));
                int n = epayFiscService.addFisc(ele.elementText("BillNum"), ele.elementText("TraNo"), ele.elementText("PayerAcct"),
                        ele.elementText("TraTime"), ele.elementText("BankTraNo"), ele.elementText("BankStation"),
                        ele.elementText("Money"));
                if (n < nRet) nRet = n;
            }
            if (nRet > 0) {
                return new BaseResult("<retCode>0000</retCode><retMsg>success</retMsg>");
            } else if (nRet == -11) {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujujiaoyanshibai]</retMsg>");
            }  else {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujucunchushibai]</retMsg>");
            }
        }else if(head.equals("setPayFiscCheck")){
            // 电子缴库接口，人行对账
            Element eleRoot = body.element("bills");
            List<Element> bills = eleRoot.elements("bill");
            EpayFiscService epayFiscService = (EpayFiscService) SpringContextUtil.getBean("epayFiscService");
            int nRet = 1;
            for (int i=0;i<bills.size();i++) {
                Element ele = bills.get(i);
                int n = epayFiscService.setCheck(ele.elementText("BillNum"), ele.elementText("ChkAcctOrd"), ele.elementText("ChkDate"), ele.elementText("State"));
                if (n < nRet) nRet = n;
            }
            if (nRet > 0) {
                return new BaseResult("<retCode>0000</retCode><retMsg>success</retMsg>");
            } else if (nRet == -11) {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujujiaoyanshibai]</retMsg>");
            }  else {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujucunchushibai]</retMsg>");
            }
        } else if(head.equals("setPayFiscBack")){
            // 电子缴库接口，退款
            Element eleRoot = body.element("bills");
            List<Element> bills = eleRoot.elements("bill");
            EpayFiscService epayFiscService = (EpayFiscService) SpringContextUtil.getBean("epayFiscService");
            int nRet = 1;
            for (int i=0;i<bills.size();i++) {
                Element ele = bills.get(i);
                int n = epayFiscService.setBackPay(ele.elementText("BillNum"), ele.elementText("BackTraNo"), ele.elementText("BackTime"),
                        ele.elementText("Money"));
                if (n < nRet) nRet = n;
            }
            if (nRet > 0) {
                return new BaseResult("<retCode>0000</retCode><retMsg>success</retMsg>");
            } else if (nRet == -11) {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujujiaoyanshibai]</retMsg>");
            }  else {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujucunchushibai]</retMsg>");
            }
        } else if(head.equals("setBankPayment")){
            // 电子缴库接口，划款
            Element eleRoot = body.element("bills");
            List<Element> bills = eleRoot.elements("bill");
            EpayFiscService epayFiscService = (EpayFiscService) SpringContextUtil.getBean("epayFiscService");
            int nRet = 1;
            for (int i=0;i<bills.size();i++) {
                Element ele = bills.get(i);
                int n = epayFiscService.setPayment(ele.elementText("BillNum"), ele.elementText("TraDate"));
                if (n < nRet) nRet = n;
            }
            if (nRet > 0) {
                return new BaseResult("<retCode>0000</retCode><retMsg>success</retMsg>");
            } else if (nRet == -11) {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujujiaoyanshibai]</retMsg>");
            }  else {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujucunchushibai]</retMsg>");
            }
        } else if(head.equals("setPayFiscUnited")){
            // 电子缴库接口，扣款对账统一流水
            Element eleRoot = body.element("bills");
            List<Element> bills = eleRoot.elements("bill");
            EpayFiscService epayFiscService = (EpayFiscService) SpringContextUtil.getBean("epayFiscService");
            int nRet = 1;
            for (int i=0;i<bills.size();i++) {
                Element ele = bills.get(i);
                EpayFiscVO vo = new EpayFiscVO();
                vo.setBillNum(ele.elementText("BillNum"));
                vo.setTraNo(ele.elementText("TraNo"));
                vo.setAccountCode(ele.elementText("PayerAcct"));
                vo.setPayBankDt(ele.elementText("TraTime"));
                vo.setBankAffirm(ele.elementText("BankTraNo"));
                vo.setBankStation(ele.elementText("BankStation"));
                vo.setMoney(new BigDecimal(ele.elementText("Money")));
                vo.setChkAcctOrd(ele.elementText("ChkAcctOrd"));
                vo.setCheckDate(ele.elementText("ChkDate"));
                int n = epayFiscService.addFiscUnited(vo,ele.elementText("State"));
                if (n < nRet) nRet = n;
            }
            if (nRet > 0) {
                return new BaseResult("<retCode>0000</retCode><retMsg>success</retMsg>");
            } else if (nRet == -11) {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujujiaoyanshibai]</retMsg>");
            }  else {
                return new BaseResult("<retCode>0001</retCode><retMsg>fail:[shujucunchushibai]</retMsg>");
            }
        }

        return null;
    }
}
